/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa036is.daoimplementation;

import cz.muni.fi.pa036is.daointerface.ComplaintDAO;
import cz.muni.fi.pa036is.entity.Complaint;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Kuba
 */
@Repository
public class ComplaintDAOImpl implements ComplaintDAO {

    private Connection con = null;
    private Statement st = null;
    private ResultSet rs = null;

    public ComplaintDAOImpl() throws ClassNotFoundException {

        String url = "jdbc:postgresql://localhost/PA036";
        String user = "postgres";
        String password = "test";

        try {
												Class.forName("org.postgresql.Driver");
            con = DriverManager.getConnection(url, user, password);
        } catch (SQLException ex) {
            Logger.getLogger(ComplaintDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void addComplaint(Complaint complaint) {
        try {

            String insert = "INSERT INTO complaint"
                    + "(match_id, status, date, name, text) VALUES"
                    + "(?,?,?,?,?)";
            PreparedStatement preparedStatement = con.prepareStatement(insert, Statement.RETURN_GENERATED_KEYS);


            preparedStatement.setLong(1, complaint.getMatchId());
            preparedStatement.setInt(2, complaint.getStatus());
            preparedStatement.setTimestamp(3, complaint.getTimestamp());
            preparedStatement.setString(4, complaint.getName());
            preparedStatement.setString(5, complaint.getText());
            preparedStatement.executeUpdate();
												
												ResultSet rs = preparedStatement.getGeneratedKeys();
												rs.next();
												Long complaintId = rs.getLong(1);
												complaint.setId(complaintId);

        } catch (SQLException ex) {
            Logger.getLogger(ComplaintDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void editComplaint(Complaint complaint) {
        try {

            String update = "UPDATE complaint SET match_id= ?, status= ?, date= ?, name= ?, text= ? WHERE id= ?";

            PreparedStatement preparedStatement = con.prepareStatement(update);

            preparedStatement.setLong(1, complaint.getMatchId());
            preparedStatement.setInt(2, complaint.getStatus());
            preparedStatement.setTimestamp(3, complaint.getTimestamp());
            preparedStatement.setString(4, complaint.getName());
            preparedStatement.setString(5, complaint.getText());
            preparedStatement.setLong(6, complaint.getId());

            preparedStatement.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ComplaintDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void deleteComplaint(Long id) {
        try {

            String delete = "DELETE FROM complaint WHERE id=?";

            PreparedStatement preparedStatement = con.prepareStatement(delete);

            preparedStatement.setLong(1, id);

            preparedStatement.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ComplaintDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Complaint findComplaint(Long id) {
        Complaint complaint = new Complaint();
        try {

            String find = "SELECT * FROM complaint WHERE id=?";

            PreparedStatement preparedStatement = con.prepareStatement(find);

            preparedStatement.setLong(1, id);

            rs = preparedStatement.executeQuery();

            rs.next();
            complaint.setId(rs.getLong(1));
            complaint.setMatchId(rs.getLong(2));
            complaint.setStatus(rs.getInt(3));
            complaint.setTimestamp(rs.getTimestamp(4));
            complaint.setName(rs.getString(5));
            complaint.setText(rs.getString(6));

        } catch (SQLException ex) {
            Logger.getLogger(ComplaintDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return complaint;
    }

    @Override
    public List<Complaint> getAllComplaintsForMatch(Long id) {
        List<Complaint> complaintList = new ArrayList<Complaint>();

        try {

            String getAll = "SELECT * FROM complaint WHERE match_id= ?";

            PreparedStatement preparedStatement = con.prepareStatement(getAll);

            preparedStatement.setLong(1, id);

            rs = preparedStatement.executeQuery();

            while (rs.next()) {
                Complaint complaint = new Complaint();
                complaint.setId(rs.getLong(1));
                complaint.setMatchId(rs.getLong(2));
                complaint.setStatus(rs.getInt(3));
                complaint.setTimestamp(rs.getTimestamp(4));
                complaint.setName(rs.getString(5));
                complaint.setText(rs.getString(6));
                complaintList.add(complaint);

            }
        } catch (SQLException ex) {
            Logger.getLogger(ComplaintDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return complaintList;
    }

    @Override
    public List<Complaint> getAllComplaints() {
        List<Complaint> complaintList = new ArrayList<Complaint>();

        try {

            String getAll = "SELECT * FROM complaint";

            PreparedStatement preparedStatement = con.prepareStatement(getAll);


            rs = preparedStatement.executeQuery();

            while (rs.next()) {
                Complaint complaint = new Complaint();
                complaint.setId(rs.getLong(1));
                complaint.setMatchId(rs.getLong(2));
                complaint.setStatus(rs.getInt(3));
                complaint.setTimestamp(rs.getTimestamp(4));
                complaint.setName(rs.getString(5));
                complaint.setText(rs.getString(6));
                complaintList.add(complaint);

            }
        } catch (SQLException ex) {
            Logger.getLogger(ComplaintDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return complaintList;
    }
				
				@Override
				public List<Complaint> getComplaintsByMatch(Long id) {
								List<Complaint> complaintList = new ArrayList<Complaint>();

        try {

            String getAll = "SELECT * FROM complaint WHERE match_id = ?";

            PreparedStatement preparedStatement = con.prepareStatement(getAll);

												preparedStatement.setLong(1, id);

            rs = preparedStatement.executeQuery();

            while (rs.next()) {
                Complaint complaint = new Complaint();
                complaint.setId(rs.getLong(1));
                complaint.setMatchId(rs.getLong(2));
                complaint.setStatus(rs.getInt(3));
                complaint.setTimestamp(rs.getTimestamp(4));
                complaint.setName(rs.getString(5));
                complaint.setText(rs.getString(6));
                complaintList.add(complaint);

            }
        } catch (SQLException ex) {
            Logger.getLogger(ComplaintDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return complaintList;
				}
}
